<script setup lang="ts">
import { ref } from 'vue'
import KnowledgeCard from './KnowledgeCard.vue'
import type {
  KnowledgeList,
  KnowledgeParams,
  KnowledgeType,
} from '@/types/consult'
import { getKnowledgePage } from '@/services/consult'

const props = defineProps<{
  type: KnowledgeType
}>()
//加载中的状态
const loading = ref(false)

//是否完全加载完毕数据
const finished = ref(false)
//  数据列表
const list = ref<KnowledgeList>([])

// 查询阐述
const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1,
  pageSize: 5,
})

//滚动到底部
const onLoad = async () => {
  // 加载后台数据  (拿到数据)
  const res = await getKnowledgePage(params.value)
  // 往list追加
  list.value.push(...res.data.rows)

  //判断已经加载完毕  当前页码比总页数大
  if (params.value.current >= res.data.pageTotal) {
    finished.value = true
  } else {
    //没有加载完 页面++
    params.value.current++
  }
  // 加载完毕  loading 改为false
  loading.value = false
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!-- item 传给Card  知识信息对象-->
      <knowledge-card
        v-for="item in list"
        :key="item.id"
        :item="item"
      ></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
